package com.gh.ggkt.vod.controller;


import com.atguigu.ggkt.model.vod.Chapter;
import com.atguigu.ggkt.vo.vod.ChapterVo;
import com.gh.Result;
import com.gh.ggkt.vod.service.ChapterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Eric Fang
 * @since 2022-09-11
 */
@RestController
@RequestMapping("/admin/vod/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    /**
     * 大纲列表（章节和小节列表）
     */
    @ApiOperation("大纲列表")
    @GetMapping("/getNestedTreeList/{courseId}")
    public Result getNestedTreeList(@PathVariable Long courseId) {
        List<ChapterVo> chapterVos = chapterService.getTreeList(courseId);
        return Result.ok(chapterVos);
    }

    /**
     * 添加章节
     */
    @PostMapping("save")
    public Result saveChapter(@RequestBody Chapter chapter) {
        chapterService.save(chapter);
        return Result.ok(null);
    }

    /**
     * 通过id获取
     */
    @GetMapping("get/{id}")
    public Result getChapterById(@PathVariable Long id) {
        Chapter chapter = chapterService.getById(id);
        return Result.ok(chapter);
    }

    /**
     * 修改
     */
    @PostMapping("update")
    public Result update(@RequestBody Chapter chapter) {
        chapterService.updateById(chapter);
        return Result.ok(null);
    }

    /**
     * 删除
     */
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean flag = chapterService.removeById(id);
        if (flag) {
            return Result.ok(null).message("删除成功");
        }
        return Result.fail(null).message("删除失败");
    }
}

